package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class HorizontalRecyclerView extends RecyclerView {
    private boolean isLock;
    private float firstMoveX;
    private float firstMoveY;
    private boolean isFirstMove = true;

    public HorizontalRecyclerView(@NonNull Context context) {
        super(context);
    }

    public HorizontalRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_MOVE:
                if (isFirstMove) {
                    isFirstMove = false;
                    firstMoveX = event.getRawX();
                    firstMoveY = event.getRawY();
                } else {
                    float rawX = event.getRawX();
                    float rawY = event.getRawY();

                    //向下滑动 且 向下滑动的距离 大于 项左或者右滑动的距离，判断为向下滑动，不消费此次事件
                    if (rawY - firstMoveY > 0 && rawY - firstMoveY > Math.abs(rawX - firstMoveX)) {
                        isFirstMove = true;
                        return false;
                    }

                }


                break;
        }

        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_MOVE:
                if (isFirstMove) {
                    isFirstMove = false;
                    firstMoveX = event.getRawX();
                    firstMoveY = event.getRawY();
                } else {
                    float rawX = event.getRawX();
                    float rawY = event.getRawY();

                    //向下滑动 且 向下滑动的距离 大于 项左或者右滑动的距离，判断为向下滑动，不消费此次事件
                    if (rawY - firstMoveY > 0 && rawY - firstMoveY > Math.abs(rawX - firstMoveX)) {
                        isFirstMove = true;
                        return false;
                    }
                }
                break;
        }

        return super.onTouchEvent(event);
    }


    //    @Override
//    public boolean dispatchTouchEvent(MotionEvent event) {
//        switch (event.getActionMasked()) {
////            case MotionEvent.ACTION_DOWN:// 非第一个触点按下
////                isLock = true;
////                break;
//            case MotionEvent.ACTION_MOVE:
//                if (isFirstMove) {
//                    isFirstMove = false;
//                    firstMoveX = event.getRawX();
//                    firstMoveY = event.getRawY();
//                } else {
//                    float rawX = event.getRawX();
//                    float rawY = event.getRawY();
//
//
//                    //向下滑动 且 向下滑动的距离 大于 项左或者右滑动的距离，判断为向下滑动，不消费此次事件
//                    if (rawY - firstMoveY > 0 && rawY - firstMoveY > Math.abs(rawX - firstMoveX)) {
//                        isFirstMove = true;
//                        return false;
//                    }
//
//                }
//
//
//                break;
//        }
//        return super.dispatchTouchEvent(event);
//    }


}
